package com.ev.k3cloud.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ev.common.jobs.NoticeJob;
import com.ev.framework.utils.OkHttpUtil;
import com.ev.k3cloud.config.K3CloudConfig;
import com.ev.k3cloud.enums.K3CloudWebApiOperator;
import com.ev.k3cloud.service.K3CloudService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service
public class K3CloudServiceImpl implements K3CloudService {
    @Autowired
    K3CloudConfig k3CloudConfig;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final Logger log= LoggerFactory.getLogger(NoticeJob.class);

    @Override
    public void login() {
        Map<String, String> param = new HashMap<>();
        param.put("acctID", k3CloudConfig.getAcctId());
        param.put("username", k3CloudConfig.getUserName());
        param.put("password", k3CloudConfig.getPassword());
        param.put("lcid", k3CloudConfig.getLcid());
        String url = k3CloudConfig.getBaseUrl() + K3CloudWebApiOperator.LOGIN.getUrl();
        //调用金蝶登录接口获取cookie
        Map<String, String> resultMap = OkHttpUtil.postJsonParamsForK3Cloud(url, JSON.toJSONString(param), null);
        if (resultMap == null) {
            log.error("K3Cloud登录失败。");
            return;
        }
        JSONObject obj = JSON.parseObject(resultMap.get("body"));
        if (!obj.getString("LoginResultType").equals("1")) {
            log.error(obj.getString("Message"));
            return;
        }
        //缓存cookie
        if (obj.getString("LoginResultType").equals("1")) {
            stringRedisTemplate.opsForValue().set("K3Cloud_Cookie", resultMap.get("cookie"));
        }
        return;
    }

    @Override
    public String view(String param) {
        String url = k3CloudConfig.getBaseUrl() + K3CloudWebApiOperator.VIEW.getUrl();
        return doRequest(url, param);
    }

    @Override
    public String billQuery(String param) {
        String url = k3CloudConfig.getBaseUrl() + K3CloudWebApiOperator.BILL_QUERY.getUrl();
        return doRequest(url, param);
    }

    @Override
    public String save(String param) {
        String url = k3CloudConfig.getBaseUrl() + K3CloudWebApiOperator.SAVE.getUrl();
        return doRequest(url, param);
    }

    @Override
    public String batchSave(String param) {
        String url = k3CloudConfig.getBaseUrl() + K3CloudWebApiOperator.BATCH_SAVE.getUrl();
        return doRequest(url, param);
    }

    @Override
    public String submit(String param) {
        String url = k3CloudConfig.getBaseUrl() + K3CloudWebApiOperator.SUBMIT.getUrl();
        return doRequest(url, param);
    }

    @Override
    public String audit(String param) {
        String url = k3CloudConfig.getBaseUrl() + K3CloudWebApiOperator.AUDIT.getUrl();
        return doRequest(url, param);
    }

    @Override
    public String unAudit(String param) {
        String url = k3CloudConfig.getBaseUrl() + K3CloudWebApiOperator.UN_AUDIT.getUrl();
        return doRequest(url, param);
    }

    @Override
    public String delete(String param) {
        String url = k3CloudConfig.getBaseUrl() + K3CloudWebApiOperator.DELETE.getUrl();
        return doRequest(url, param);
    }

    @Override
    public String push(String param) {
        String url = k3CloudConfig.getBaseUrl() + K3CloudWebApiOperator.PUSH.getUrl();
        return doRequest(url, param);
    }

    public String doRequest(String url, String param) {
        String cookie = stringRedisTemplate.opsForValue().get("K3Cloud_Cookie");
        if (cookie != null && !cookie.isEmpty()) {
            Map<String, String> map = new HashMap<>();
            map.put("Cookie", cookie);
            return OkHttpUtil.postJsonParams(url, param, map);
        }
        return null;
    }
}
